from sanic import response

from model.guestModel import search_guest, update_guest, delete_guest, insert_guest
from model.deviceModel import search_user_link_dev


# 客户管理
async def guest_management(request):    # 只需要客户名，合同数，设备数
        base_info = search_guest()

        guestName_list = [{'id': base_info[i]['id'], 'company_name': base_info[i]['company_name']}
                          for i in range(len(base_info))]

        # <<<<<<<<<<<<<<<<<<<<<<<<合同数，设备数需要从合同中找
        return response.json(guestName_list)


# 客户详情
async def guest(reuqest):       # <<<<<<<<<<<<<<<<<<<<<<<<<<<还差合同信息
        company_id = reuqest.form.get('company_id')
        base_info = search_guest(company_id=company_id)

        # <<<<<<<<<<<<<<<<<<<<<<<<合同数，设备数需要从合同中找

        return response.json(base_info)


# 客户基本信息
async def guest_info(request):  # 考虑到客户的惰性，我们通过卖家直接给客户注册账号，然后添加合同号。客户账号，权限默认为最高级A，密码默认为手机号后六位。
        company_id = request.form.get('company_id')
        base_info = search_guest(company_id=company_id)

        # <<<<<<<<<<<<<<<<<<<<<<<<没有联系人，联系人号码
        # <<<<<<<<<<<<<<<<<<<<<<<<多了公司邮箱
        return response.json(base_info)


# 添加客户
async def add_guest(request):   # 考虑到客户的惰性，我们通过卖家直接给客户注册账号，然后添加合同号。客户账号，权限默认为最高级A，密码默认为手机号后六位。
        # legal_name和legal_tel默认不输入与contact相同
        company_name = request.form.get('company_name')
        # contact_name = request.form.get('contact_name')
        # contact_tel = request.form.get('contact_tel')
        tax_payer = request.form.get('tax_payer')
        legal_name = request.form.get('legal_name')
        legal_tel = request.form.get('legal_tel')
        company_addr = request.form.get('company_addr')
        company_email = request.form.get('company_email')

        success = insert_guest(company_name=company_name, tax_payer=tax_payer, legal_name=legal_name,
                               legal_tel=legal_tel, company_addr=company_addr)
        if success:
                return response.text('add success!')
        else:
                return response.text('add fail!')


# 编辑客户信息
async def edit_guest(request):
        company_id = request.form.get('company_id')

        company_name = request.form.get('company_name')
        # contact_name = request.form.get('contact_name')
        # contact_tel = request.form.get('contact_tel')
        tax_payer = request.form.get('tax_payer')
        legal_name = request.form.get('legal_name')
        legal_tel = request.form.get('legal_tel')
        company_addr = request.form.get('company_addr')

        success = update_guest(company_id=company_id, company_name=company_name,
                                  tax_payer=tax_payer, legal_name=legal_name,
                                  legal_tel=legal_tel, company_addr=company_addr)
        if success:
                return response.text('edit success!')
        else:
                return response.text('edit fail!')


async def delete_guest(request):
        company_id = request.form.get('company_id')
        print('company_id:', company_id)
        success = delete_guest(company_id=company_id)
        if success:
                return response.text('delete success!')
        else:
                return response.text('delete fail!')
